package io.chenglicun.modules.clc.controller;

import com.alibaba.fastjson.JSONObject;
import io.chenglicun.common.annotation.LogOperation;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.utils.ExcelUtils;
import io.chenglicun.common.utils.Result;
import io.chenglicun.common.validator.AssertUtils;
import io.chenglicun.common.validator.ValidatorUtils;
import io.chenglicun.common.validator.group.AddGroup;
import io.chenglicun.common.validator.group.DefaultGroup;
import io.chenglicun.common.validator.group.UpdateGroup;
import io.chenglicun.modules.clc.dto.ClcMapNodeInfoDTO;
import io.chenglicun.modules.clc.excel.ClcMapNodeInfoExcel;
import io.chenglicun.modules.clc.service.ClcMapNodeInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
* 数字地图节点信息
*
* @author HB 137664951@qq.com
* @since 1.0 2023-11-06
*/
@RestController
@RequestMapping("clc/clcmapnodeinfo")
@Api(tags="数字地图节点信息")
public class ClcMapNodeInfoController {
    @Autowired
    private ClcMapNodeInfoService clcMapNodeInfoService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = "sourceId", value = "来源id", paramType = "query", dataType="long") ,
        @ApiImplicitParam(name = "sourceType", value = "来源类型", paramType = "query", dataType="int") ,
        @ApiImplicitParam(name = "annotationType", value = "标注类型", paramType = "query", dataType="String")
    })
    @RequiresPermissions("clc:clcmapnodeinfo:page")
    public Result<PageData<ClcMapNodeInfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<ClcMapNodeInfoDTO> page = clcMapNodeInfoService.page(params);

        return new Result<PageData<ClcMapNodeInfoDTO>>().ok(page);
    }


    @GetMapping("getInfoByPointId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pointId", value = "标注Id", required = true, paramType = "query", dataType="String")
    })
    @ApiOperation("获取标注")
    public Result<Object> getInfo(@ApiIgnore @RequestParam Map<String, Object> params){

        return new Result<>().ok(clcMapNodeInfoService.getInfo(params.get("pointId").toString()));
    }

    @GetMapping("getInfo")
    @ApiOperation("获取标注")
    public Result<Object> getInfo(){
        Map<String, Object> map = clcMapNodeInfoService.getList();
        return new Result<Object>().ok(map);
        /*String data = "{\n" +
                "            type:\"draw\",\n" +
                "            widgets:\"plot-api\",\n" +
                "            data: {\n" +
                "              type:\"FeatureCollection\",\n" +
                "              features:[{\n" +
                "                type:\"Feature\",\n" +
                "                properties:{\n" +
                "                  edittype:\"polygon_clampToGround\",\n" +
                "                  name:\"贴地面\",\n" +
                "                  position:{height:false, minCount:3},\n" +
                "                  style:{},\n" +
                "                  attr:{id:\"20240109180955\"},\n" +
                "                  type:\"polygon\"},\n" +
                "                geometry:\n" +
                "                  {\n" +
                "                    type:\"Polygon\",\n" +
                "                    coordinates:[[[119.83057,31.235591,0], [119.833769,31.233616,0], [119.835255,31.236735,0], [119.833866,31.237322,0], [119.83057,31.235591,0]]]}\n" +
                "              }]}}";
        JSONObject jsonObject = JSONObject.parseObject(data);
        String data1 = "{ \"type\": \"FeatureCollection\", \"features\": [ { \"type\": \"Feature\", \"properties\": { \"edittype\": \"polygon_clampToGround\", \"name\": \"贴地面\", \"position\": { \"height\": false, \"minCount\": 3 }, \"style\": { \"color\": \"#ffff00\", \"opacity\": 0.6, \"clampToGround\": true, \"zIndex\": 0 }, \"attr\": { \"id\": \"20240109212349\", \"name\": \"\", \"remark\": \"\" }, \"type\": \"polygon\" }, \"geometry\": { \"type\": \"Polygon\", \"coordinates\": [ [ [ 119.833089, 31.240544, 0 ], [ 119.830864, 31.234186, 0 ], [ 119.844147, 31.233338, 0 ], [ 119.844525, 31.239959, 0 ], [ 119.847438, 31.23982, 0 ], [ 119.833089, 31.240544, 0 ] ] ] } } ] }";

        JSONObject jsonObject1 = JSONObject.parseObject(data1);

        ArrayList<Object> list = new ArrayList<>();
        list.add(jsonObject);
        list.add(jsonObject1);

        String data2 = "{\n" +
                "    \"type\": \"FeatureCollection\",\n" +
                "    \"features\": [\n" +
                "        {\n" +
                "            \"type\": \"Feature\",\n" +
                "            \"properties\": {\n" +
                "                \"edittype\": \"polyline\",\n" +
                "                \"name\": \"线\",\n" +
                "                \"position\": {\n" +
                "                    \"minCount\": 2\n" +
                "                },\n" +
                "                \"style\": {},\n" +
                "                \"attr\": {\n" +
                "                    \"id\": \"20220804154955\"\n" +
                "                },\n" +
                "                \"type\": \"polyline\"\n" +
                "            },\n" +
                "            \"geometry\": {\n" +
                "                \"type\": \"LineString\",\n" +
                "                \"coordinates\": [\n" +
                "                    [\n" +
                "                        120.428936,\n" +
                "                        31.491583,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.430502,\n" +
                "                        31.491733,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.43302,\n" +
                "                        31.492198,\n" +
                "                        0\n" +
                "                    ]\n" +
                "                ]\n" +
                "            }\n" +
                "        },\n" +
                "        {\n" +
                "            \"type\": \"Feature\",\n" +
                "            \"properties\": {\n" +
                "                \"edittype\": \"polyline\",\n" +
                "                \"name\": \"线\",\n" +
                "                \"position\": {\n" +
                "                    \"minCount\": 2,\n" +
                "                    \"height\": true\n" +
                "                },\n" +
                "                \"style\": {},\n" +
                "                \"attr\": {\n" +
                "                    \"id\": \"20220804155001\"\n" +
                "                },\n" +
                "                \"type\": \"polyline\"\n" +
                "            },\n" +
                "            \"geometry\": {\n" +
                "                \"type\": \"LineString\",\n" +
                "                \"coordinates\": [\n" +
                "                    [\n" +
                "                        120.429146,\n" +
                "                        31.490781,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.430624,\n" +
                "                        31.490666,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.432948,\n" +
                "                        31.49107,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.433716,\n" +
                "                        31.491291,\n" +
                "                        0\n" +
                "                    ]\n" +
                "                ]\n" +
                "            }\n" +
                "        },\n" +
                "        {\n" +
                "            \"type\": \"Feature\",\n" +
                "            \"properties\": {\n" +
                "                \"edittype\": \"polyline\",\n" +
                "                \"name\": \"线\",\n" +
                "                \"position\": {\n" +
                "                    \"minCount\": 2,\n" +
                "                    \"height\": true\n" +
                "                },\n" +
                "                \"style\": {},\n" +
                "                \"attr\": {\n" +
                "                    \"id\": \"20220804155005\"\n" +
                "                },\n" +
                "                \"type\": \"polyline\"\n" +
                "            },\n" +
                "            \"geometry\": {\n" +
                "                \"type\": \"LineString\",\n" +
                "                \"coordinates\": [\n" +
                "                    [\n" +
                "                        120.429326,\n" +
                "                        31.48946,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.431708,\n" +
                "                        31.489748,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.434476,\n" +
                "                        31.490176,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.435574,\n" +
                "                        31.490313,\n" +
                "                        0\n" +
                "                    ]\n" +
                "                ]\n" +
                "            }\n" +
                "        },\n" +
                "        {\n" +
                "            \"type\": \"Feature\",\n" +
                "            \"properties\": {\n" +
                "                \"edittype\": \"polyline\",\n" +
                "                \"name\": \"线\",\n" +
                "                \"position\": {\n" +
                "                    \"minCount\": 2,\n" +
                "                    \"height\": true\n" +
                "                },\n" +
                "                \"style\": {},\n" +
                "                \"attr\": {\n" +
                "                    \"id\": \"20220804155009\"\n" +
                "                },\n" +
                "                \"type\": \"polyline\"\n" +
                "            },\n" +
                "            \"geometry\": {\n" +
                "                \"type\": \"LineString\",\n" +
                "                \"coordinates\": [\n" +
                "                    [\n" +
                "                        120.429795,\n" +
                "                        31.487746,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.432528,\n" +
                "                        31.488221,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.434063,\n" +
                "                        31.488452,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.435582,\n" +
                "                        31.488663,\n" +
                "                        0\n" +
                "                    ]\n" +
                "                ]\n" +
                "            }\n" +
                "        },\n" +
                "        {\n" +
                "            \"type\": \"Feature\",\n" +
                "            \"properties\": {\n" +
                "                \"edittype\": \"polyline\",\n" +
                "                \"name\": \"线\",\n" +
                "                \"position\": {\n" +
                "                    \"minCount\": 2,\n" +
                "                    \"height\": true\n" +
                "                },\n" +
                "                \"style\": {},\n" +
                "                \"attr\": {\n" +
                "                    \"id\": \"20220804155016\"\n" +
                "                },\n" +
                "                \"type\": \"polyline\"\n" +
                "            },\n" +
                "            \"geometry\": {\n" +
                "                \"type\": \"LineString\",\n" +
                "                \"coordinates\": [\n" +
                "                    [\n" +
                "                        120.429951,\n" +
                "                        31.485497,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.433133,\n" +
                "                        31.486702,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.435061,\n" +
                "                        31.487038,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.436338,\n" +
                "                        31.486579,\n" +
                "                        0\n" +
                "                    ]\n" +
                "                ]\n" +
                "            }\n" +
                "        },\n" +
                "        {\n" +
                "            \"type\": \"Feature\",\n" +
                "            \"properties\": {\n" +
                "                \"edittype\": \"polyline\",\n" +
                "                \"name\": \"线\",\n" +
                "                \"position\": {\n" +
                "                    \"minCount\": 2,\n" +
                "                    \"height\": true\n" +
                "                },\n" +
                "                \"style\": {},\n" +
                "                \"attr\": {\n" +
                "                    \"id\": \"20220804155020\"\n" +
                "                },\n" +
                "                \"type\": \"polyline\"\n" +
                "            },\n" +
                "            \"geometry\": {\n" +
                "                \"type\": \"LineString\",\n" +
                "                \"coordinates\": [\n" +
                "                    [\n" +
                "                        120.429303,\n" +
                "                        31.484877,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.432444,\n" +
                "                        31.484583,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.435112,\n" +
                "                        31.484958,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.439132,\n" +
                "                        31.485889,\n" +
                "                        0\n" +
                "                    ]\n" +
                "                ]\n" +
                "            }\n" +
                "        },\n" +
                "        {\n" +
                "            \"type\": \"Feature\",\n" +
                "            \"properties\": {\n" +
                "                \"edittype\": \"polyline\",\n" +
                "                \"name\": \"线\",\n" +
                "                \"position\": {\n" +
                "                    \"minCount\": 2,\n" +
                "                    \"height\": true\n" +
                "                },\n" +
                "                \"style\": {},\n" +
                "                \"attr\": {\n" +
                "                    \"id\": \"20220804155024\"\n" +
                "                },\n" +
                "                \"type\": \"polyline\"\n" +
                "            },\n" +
                "            \"geometry\": {\n" +
                "                \"type\": \"LineString\",\n" +
                "                \"coordinates\": [\n" +
                "                    [\n" +
                "                        120.43157,\n" +
                "                        31.482805,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.433129,\n" +
                "                        31.483184,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.435715,\n" +
                "                        31.483812,\n" +
                "                        0\n" +
                "                    ]\n" +
                "                ]\n" +
                "            }\n" +
                "        },\n" +
                "        {\n" +
                "            \"type\": \"Feature\",\n" +
                "            \"properties\": {\n" +
                "                \"edittype\": \"polyline\",\n" +
                "                \"name\": \"线\",\n" +
                "                \"position\": {\n" +
                "                    \"minCount\": 2,\n" +
                "                    \"height\": true\n" +
                "                },\n" +
                "                \"style\": {},\n" +
                "                \"attr\": {\n" +
                "                    \"id\": \"20220804155032\"\n" +
                "                },\n" +
                "                \"type\": \"polyline\"\n" +
                "            },\n" +
                "            \"geometry\": {\n" +
                "                \"type\": \"LineString\",\n" +
                "                \"coordinates\": [\n" +
                "                    [\n" +
                "                        120.430594,\n" +
                "                        31.481783,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.43329,\n" +
                "                        31.481859,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.436254,\n" +
                "                        31.482393,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.437376,\n" +
                "                        31.483125,\n" +
                "                        0\n" +
                "                    ],\n" +
                "                    [\n" +
                "                        120.438544,\n" +
                "                        31.48331,\n" +
                "                        0\n" +
                "                    ]\n" +
                "                ]\n" +
                "            }\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        JSONObject jsonObject2 = JSONObject.parseObject(data2);*/

//        return new Result<Object>().ok(jsonObject2);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("clc:clcmapnodeinfo:info")
    public Result<ClcMapNodeInfoDTO> get(@PathVariable("id") Long id){
        ClcMapNodeInfoDTO data = clcMapNodeInfoService.get(id);

        return new Result<ClcMapNodeInfoDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
//    @RequiresPermissions("clc:clcmapnodeinfo:save")
    public Result save(@RequestBody ClcMapNodeInfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        clcMapNodeInfoService.save(dto);

        return new Result();



    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("clc:clcmapnodeinfo:update")
    public Result update(@RequestBody ClcMapNodeInfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        clcMapNodeInfoService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("clc:clcmapnodeinfo:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        clcMapNodeInfoService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("clc:clcmapnodeinfo:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<ClcMapNodeInfoDTO> list = clcMapNodeInfoService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "数字地图节点信息", list, ClcMapNodeInfoExcel.class);
    }

}